package com.renzhichu.app.polyv;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.ContentObserver;
import android.graphics.Color;
import android.os.Build;
import android.os.Handler;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;

import com.blankj.utilcode.util.ToastUtils;
import com.easefun.polyv.businesssdk.model.video.PolyvBaseVideoParams;
import com.easefun.polyv.businesssdk.model.video.PolyvCloudClassVideoParams;
import com.easefun.polyv.businesssdk.model.video.PolyvPlaybackVideoParams;
import com.easefun.polyv.businesssdk.vodplayer.PolyvVodSDKClient;
import com.easefun.polyv.cloudclass.chat.PolyvChatManager;
import com.easefun.polyv.cloudclass.config.PolyvLiveSDKClient;
import com.easefun.polyv.cloudclassdemo.watch.player.PolyvOrientoinListener;
import com.easefun.polyv.cloudclassdemo.watch.player.live.PolyvCloudClassVideoHelper;
import com.easefun.polyv.cloudclassdemo.watch.player.live.PolyvCloudClassVideoItem;
import com.easefun.polyv.cloudclassdemo.watch.player.live.widget.IPolyvVideoViewPlayListener;
import com.easefun.polyv.cloudclassdemo.watch.player.live.widget.IPolyvVideoViewVodListener;
import com.easefun.polyv.cloudclassdemo.watch.player.playback.PolyvPlaybackVideoHelper;
import com.easefun.polyv.cloudclassdemo.watch.player.playback.PolyvPlaybackVideoItem;
import com.easefun.polyv.commonui.player.widget.PolyvSlideSwitchView;
import com.easefun.polyv.foundationsdk.config.PolyvPlayOption;
import com.easefun.polyv.foundationsdk.log.PolyvCommonLog;
import com.easefun.polyv.foundationsdk.utils.PolyvScreenUtils;
import com.easefun.polyv.linkmic.PolyvLinkMicClient;
import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.facebook.react.uimanager.events.RCTEventEmitter;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.sql.SQLOutput;
import java.util.Map;

import retrofit2.adapter.rxjava2.HttpException;

public class RNTPolyvLiveManager extends SimpleViewManager<RNTPolyvLive> {
    protected static final String REACT_CLASS = "RNTPolyvLiveView";
    private static final String EVENT_NAME_ONCLICK = "onClick";
    private static final String EVENT_NAME_PLAY = "onPlayLiveEvent";
    private static final String EVENT_NAME_VOD = "onPlayVodEvent";

    private static final String INIT_CONFIG_METHOD_NAME = "initConfig"; // 交互方法名
    private static final int INIT_CONFIG_METHOD_ID = 1;

    private static final String NATIVE_PROPS_METHOD_NAME = "nativeProps"; // 交互方法名
    private static final int NATIVE_PROPS_METHOD_ID = 2;

    private static final String DESTROY_METHOD_NAME = "freePlayer"; // 交互方法名
    private static final int DESTROY_METHOD_ID = 3;

    private static final String PAUSE_METHOD_NAME = "paused"; // 交互方法名
    private static final int PAUSE_METHOD_ID = 4;

    private static final String PLAY_METHOD_NAME = "playVideo"; // 交互方法名
    private static final int PLAY_METHOD_ID = 5;

    private static final String INIT_PLAY_LIVE_NAME = "initPlayLive"; // 交互方法名
    private static final int INIT_PLAY_LIVE_ID = 6;

    private static final String ORIENTATION_LISTENER_NAME = "orientationListener"; // 交互方法名
    private static final int ORIENTATION_LISTENER_ID = 7;

    private RNTPolyvLive frameLayout;
    private FrameLayout playerContainer;
    //直播与点播辅助类
    private PolyvCloudClassVideoHelper livePlayerHelper;
    private PolyvPlaybackVideoHelper playbackVideoHelper;
    private PolyvSlideSwitchView polyvSlideSwitch;

    //        private String userId = "39f04b93d9", channelId = "384046", appSecret = "588eaed407d941f09ef533c3aa3c19d3", videoId, appId = "fg32rlxjhl";
    private String userId = "", channelId = "", appSecret = "", videoId, appId = "";

    private String studentUserId;
    private String studentNickName = "";
    //参数传递相关标签
    private static final String TAG = "PolyvCloudClassHomeActivity";
    private static final String CHANNELID_KEY = "channelid";
    private static final String USERID_KEY = "userid";
    private static final String VIDEOID_KEY = "videoid";
    private static final String PLAY_TYPE_KEY = "playtype";
    private static final String NORMALLIVE = "normallive";
    private static final String NORMALLIVE_PLAYBACK = "normallive_playback";
    private static final String DEFAULT_NICKNAME = "POLYV";

    @PolyvPlayOption.PlayMode
    private int playMode;

    private PolyvChatManager chatManager = new PolyvChatManager();
    //手机重力感应相关
    private RotationObserver rotationObserver;
    private PolyvOrientoinListener orientoinListener;

    //是否是普通直播  是否直播回放
    private boolean isNormalLive, isNormalLivePlayBack = true;

    private ThemedReactContext reactContext;

    @Override
    public String getName() { // 返回了定义的View Module的名字
        return REACT_CLASS;
    }

    @Override
    protected RNTPolyvLive createViewInstance(ThemedReactContext reactContext) {
        this.reactContext = reactContext;
        this.frameLayout = new RNTPolyvLive(reactContext); // 创建一个View实例供JS使用。
        this.playerContainer = new RNTPolyvLive1(reactContext);
        frameLayout.addView(this.playerContainer);
        reactContext.addLifecycleEventListener(lifecycleEventListener);
        return this.frameLayout;
    }

    // <editor-fold defaultstate="collapsed" desc="登录处理">
    private void checkToken(String appSecret, String appId) {
        PolyvLinkMicClient.getInstance().setAppIdSecret(appId, appSecret);
        PolyvLiveSDKClient.getInstance().setAppIdSecret(appId, appSecret);
        PolyvVodSDKClient.getInstance().initConfig(appId, appSecret);
    }

    public void failedStatus(String message) {
        ToastUtils.showLong(message);
    }

    public void errorStatus(Throwable e) {
        PolyvCommonLog.exception(e);
        if (e instanceof HttpException) {
            try {
                ToastUtils.showLong(((HttpException) e).response().errorBody().string());
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        } else {
            ToastUtils.showLong(e.getMessage());
        }
    }

    // </editor-fold>

    private void initial() {
        float ratio = isNormalLive ? 9.0f / 16 : 3.0f / 4;//普通直播16:9 云课堂4:3
        PolyvScreenUtils.generateHeightByRatio(this.reactContext.getCurrentActivity(), ratio);

//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            this.reactContext.getCurrentActivity().getWindow().setStatusBarColor(Color.BLACK);
//        }
    }

    private void intialOretation() {
        //创建观察类对象
        rotationObserver = new RotationObserver(new Handler());
        if (playMode == PolyvPlayOption.PLAYMODE_VOD) {
            orientoinListener = new PolyvOrientoinListener(this.reactContext.getCurrentActivity(), playbackVideoHelper);
        } else {
            orientoinListener = new PolyvOrientoinListener(this.reactContext.getCurrentActivity(), livePlayerHelper);
        }

        boolean autoRotateOn = (Settings.System.getInt(this.reactContext.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0) == 1);
        PolyvCommonLog.d(TAG, "autoRotateOn" + autoRotateOn);
//        orientoinListener.enable();// 不管怎样都要自动旋转
        orientoinListener.disable();// 不管怎样都要自动旋转
        //检查系统是否开启自动旋转
//        if (autoRotateOn) {
//            orientoinListener.enable();
//        } else {
//            orientoinListener.disable();
//        }
    }

    // <editor-fold defaultstate="collapsed" desc="屏幕旋转开关监听器">
    private class RotationObserver extends ContentObserver {
        ContentResolver mResolver;

        /**
         * Creates a content observer.
         *
         * @param handler The handler to run {@link #onChange} on, or null if none.
         */
        public RotationObserver(Handler handler) {
            super(handler);
            mResolver = reactContext.getContentResolver();
        }

        public void startObserver() {
            mResolver.registerContentObserver(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), false, this);
        }

        public void stopObserver() {
            mResolver.unregisterContentObserver(this);
        }

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            PolyvCommonLog.d(TAG, "oreitation has changed");
            boolean autoRotateOn = (Settings.System.getInt(reactContext.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0) == 1);
            //检查系统是否开启自动旋转
            if (autoRotateOn) {
                if (orientoinListener != null) {
                    orientoinListener.enable();
                }
            } else {
                if (orientoinListener != null) {
                    orientoinListener.disable();
                }
            }

        }
    }

    // </editor-fold>
    private void initialVideo() {
        PolyvCommonLog.d(TAG, "initialVodVideo");

        // 播放器
//        playerContainer = findViewById(R.id.player_container);
//        ViewGroup.LayoutParams vlp = this.playerContainer.getLayoutParams();
//        vlp.width = ViewGroup.LayoutParams.MATCH_PARENT;
//        vlp.height = PolyvScreenUtils.getHeight();

        ViewGroup viewGroup = (ViewGroup) playerContainer.getParent();
        if (viewGroup != null)
            viewGroup.removeAllViews();
        FrameLayout.LayoutParams flp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, PolyvScreenUtils.getHeight());
        playerContainer.setLayoutParams(flp);
        viewGroup.addView(playerContainer);

//        System.out.println("initialVodVideo===>width=" + vlp.width + ",height="+vlp.height);

//        initialLiveVideo();

    }

    private boolean isStartPlay = false;

    private void initialLiveVideo() {
        PolyvCloudClassVideoItem cloudClassVideoItem = new PolyvCloudClassVideoItem(this.reactContext.getCurrentActivity());
        cloudClassVideoItem.setOnPreparedListener(new IPolyvVideoViewPlayListener() {
            @Override
            public void onPreparedListener() {
//                ToastUtils.showShort("开始直播");
                isStartPlay = true;
                onReceiveNativeEvent("streamstate", "LIVE");
            }

            @Override
            public void onNoLiveAtPresentListener() {
//                ToastUtils.showShort("暂无直播");
                if (isStartPlay) {
                    isStartPlay = false;
                    onReceiveNativeEvent("streamstate", "");
                }
            }

            @Override
            public void changeToLandscape() {
                System.out.println("RNTPolyv" + "changeToLandscape");
                onReceiveNativeEvent("fullscreen", true);
            }

            @Override
            public void changeToPortrait() {
                System.out.println("RNTPolyv" + "changeToPortrait");
                onReceiveNativeEvent("fullscreen", false);
            }
        });
//        cloudClassVideoItem.setOnSendDanmuListener((danmuMessage)->{
//            polyvChatGroupFragment.sendChatMessageByDanmu(danmuMessage);
//        });

        livePlayerHelper = new PolyvCloudClassVideoHelper(cloudClassVideoItem, null, chatManager);
        livePlayerHelper.addVideoPlayer(this.playerContainer);
        livePlayerHelper.initConfig(true);
//        livePlayerHelper.addPPT(videoPptContainer);
//
//        livePlayerHelper.addLinkMicLayout(linkMicLayout, linkMicLayoutParent);

//        linkMicLayoutParent.resetFloatViewPort();


    }

    private void playPlayLiveVideo() {
        PolyvCloudClassVideoParams cloudClassVideoParams = new PolyvCloudClassVideoParams(channelId, userId,
                studentUserId// viewerid
        );
        cloudClassVideoParams
                //是否开启暖场视频
                .buildOptions(PolyvBaseVideoParams.WAIT_AD, true)
                //是否开启跑马灯
//                .buildOptions(PolyvBaseVideoParams.MARQUEE, true)
                // TODO: 2019/3/25 请在此处填入用户的昵称
                //后台统计数据需要，填入学员昵称
                .buildOptions(PolyvBaseVideoParams.PARAMS2, studentNickName);
        livePlayerHelper.startPlay(cloudClassVideoParams);
        PolyvCommonLog.d(TAG, "cloudClassVideoParams" + "  userId=>" + userId + "  appId=>" + appId + "  appSecret=>" + appSecret + "  channelId=>" + channelId + "  videoId=>" + videoId);
    }

    private void initialPlaybackVideo() {
        PolyvPlaybackVideoItem playbackVideoItem = new PolyvPlaybackVideoItem(this.reactContext.getCurrentActivity());
        playbackVideoItem.setOnVodClickListener(new IPolyvVideoViewVodListener() {
            @Override
            public void onPlay(boolean isFirst) {
                PolyvCommonLog.d(TAG, "onPlay");
            }

            @Override
            public void onPause() {
                PolyvCommonLog.d(TAG, "onPause");
            }

            @Override
            public void onCompletion() {
                PolyvCommonLog.d(TAG, "onCompletion");
            }

            @Override
            public void changeToLandscape() {
                PolyvCommonLog.d(TAG, "changeToLandscape");
                onReceiveNativeVodEvent("fullscreen", true);
            }

            @Override
            public void changeToPortrait() {
                PolyvCommonLog.d(TAG, "changeToPortrait");
                onReceiveNativeVodEvent("fullscreen", false);
            }

            @Override
            public void goBack() {
                onReceiveNativeVodEvent("goBack", true);
            }
        });
        playbackVideoHelper = new PolyvPlaybackVideoHelper(playbackVideoItem, null);
        playbackVideoHelper.addVideoPlayer(playerContainer);
        playbackVideoHelper.initConfig(isNormalLivePlayBack);
//        playbackVideoHelper.addPPT(videoPptContainer);

        playbackVideoHelper.setNickName(studentNickName);


    }

    private void playPlaybackVideo() {
        playbackVideoHelper.resetView(isNormalLivePlayBack);

        // TODO: 2018/9/12 videoId 为直播平台的 videopoolid为点播平台的视频id
        PolyvPlaybackVideoParams playbackVideoParams = new PolyvPlaybackVideoParams(videoId,//videoId
                channelId,
                userId, studentUserId//viewerid
        );
        playbackVideoParams.buildOptions(PolyvBaseVideoParams.WAIT_AD, true)
                .buildOptions(PolyvBaseVideoParams.MARQUEE, true)
                .buildOptions(PolyvBaseVideoParams.IS_PPT_PLAY, true)
                // TODO: 2019/3/25 请在此处填入用户的昵称
                .buildOptions(PolyvBaseVideoParams.PARAMS2, studentNickName)
                .buildOptions(PolyvPlaybackVideoParams.ENABLE_ACCURATE_SEEK, true);
        playbackVideoHelper.startPlay(playbackVideoParams);
        PolyvCommonLog.d(TAG, "playbackVideoParams " + "  userId=>" + userId + "  appId=>" + appId + "  appSecret=>" + appSecret + "  channelId=>" + channelId + "  videoId=>" + videoId);
    }


    // 设置属性，一定需要加这个注解，不然不认识
    @ReactProp(name = "backgroundColor")
    public void setColor(RelativeLayout view, String color) {
        view.setBackgroundColor(Color.parseColor(color));
//        view.setColor(Color.parseColor(color));
    }


    public void onReceiveNativeEvent(String key, String value) {
        WritableMap event = Arguments.createMap();
        event.putString("key", key);
        event.putString("value", value);
        this.reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(this.frameLayout.getId(), EVENT_NAME_PLAY, event);
    }

    public void onReceiveNativeEvent(String key, Boolean value) {
        WritableMap event = Arguments.createMap();
        event.putString("key", key);
        event.putBoolean("value", value);
        this.reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(this.frameLayout.getId(), EVENT_NAME_PLAY, event);
    }

    public void onReceiveNativeVodEvent(String key, boolean value) {
        WritableMap event = Arguments.createMap();
        event.putString("key", key);
        event.putBoolean("value", value);
        this.reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(this.frameLayout.getId(), EVENT_NAME_VOD, event);
    }

    /**
     * 自定义事件
     *
     * @return
     */
    @Nullable
    @Override
    public Map getExportedCustomDirectEventTypeConstants() {
        Map map = MapBuilder.of(EVENT_NAME_PLAY, MapBuilder.of("registrationName", EVENT_NAME_PLAY));
        map.put(EVENT_NAME_VOD, MapBuilder.of("registrationName", EVENT_NAME_VOD));
        return map;
    }

    /**
     * 接收交互通知
     *
     * @return
     */
    @Nullable
    @Override
    public Map<String, Integer> getCommandsMap() {
        Map<String, Integer> map = MapBuilder.of(INIT_CONFIG_METHOD_NAME, INIT_CONFIG_METHOD_ID);
        map.put(NATIVE_PROPS_METHOD_NAME, NATIVE_PROPS_METHOD_ID);
        map.put(DESTROY_METHOD_NAME, DESTROY_METHOD_ID);
        map.put(PAUSE_METHOD_NAME, PAUSE_METHOD_ID);
        map.put(PLAY_METHOD_NAME, PLAY_METHOD_ID);
        map.put(INIT_PLAY_LIVE_NAME, INIT_PLAY_LIVE_ID);
        map.put(ORIENTATION_LISTENER_NAME, ORIENTATION_LISTENER_ID);
        return map;
    }

    /**
     * 根据命令ID，处理对应任务
     *
     * @param root
     * @param commandId
     * @param args
     */
    @Override
    public void receiveCommand(RNTPolyvLive root, int commandId, @Nullable ReadableArray args) {
        switch (commandId) {
            case INIT_CONFIG_METHOD_ID:
                initConfigParams(args);
                break;
            case INIT_PLAY_LIVE_ID:
                initPlayLive();
                break;
            case PLAY_METHOD_ID:
                playVideo();
                break;
            case NATIVE_PROPS_METHOD_ID:
                fullScreenChanged(args);
                break;
            case PAUSE_METHOD_ID:
                receivePaused(args);
                break;
            case DESTROY_METHOD_ID:
                destroyVideo();
                break;
            case ORIENTATION_LISTENER_ID:
                orientationListener(args);
                break;
            default:
                break;
        }
    }

    /**
     * 全屏变化
     *
     * @param args
     */
    private void fullScreenChanged(ReadableArray args) {
        if (args != null) {
            String name = args.getString(0);//获取第一个位置的数据
            try {
                JSONObject jsonObject = new JSONObject(name);//创建JSON对象
                if (!jsonObject.has("fullscreen")) return;
                Boolean fullscreen = jsonObject.getBoolean("fullscreen");
//                        setColor(root,backgroundColor);
                //设置大小屏切换
                if (fullscreen) {
                    if (playMode == PolyvPlayOption.PLAYMODE_LIVE) {
                        if (livePlayerHelper != null)
                            livePlayerHelper.changeToLandscape();
                    } else {
                        if (playbackVideoHelper != null) {
                            playbackVideoHelper.changeToLandscape();
                        }
                    }
                } else {
                    if(playMode == PolyvPlayOption.PLAYMODE_LIVE){
                        if (livePlayerHelper != null)
                            livePlayerHelper.changeToPortrait();
                    }else {
                        if (playbackVideoHelper != null) {
                            playbackVideoHelper.changeToPortrait();
                        }
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    private void orientationListener(ReadableArray args) {
        if (args != null) {
            String name = args.getString(0);//获取第一个位置的数据
            try {
                JSONObject jsonObject = new JSONObject(name);
                Boolean orientation = jsonObject.getBoolean("orientation");
                enableOrientation(orientation);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

    }

    /**
     *
     */
    private void enableOrientation(boolean enable) {
        if (orientoinListener != null) {
            if (enable) {
                orientoinListener.enable();
            } else {
                orientoinListener.disable();
            }
        }
    }


    private void initPlayLive() {
        if (livePlayerHelper == null) {
            initPlayVideo();
        } else {
            destroyVideo();
            initPlayVideo();
        }
    }

    /**
     * 销毁video
     */
    private void destroyVideo() {
        PolyvCommonLog.d(TAG, "destroyVideo");
        if (livePlayerHelper != null) {
            livePlayerHelper.destory();
            livePlayerHelper = null;
        }
        if (playbackVideoHelper != null) {
            playbackVideoHelper.destory();
            playbackVideoHelper = null;
        }
        if (orientoinListener != null) {
            orientoinListener.disable();
            orientoinListener = null;
        }
    }

    /**
     * 接受暂停指令
     *
     * @param args
     */
    private void receivePaused(ReadableArray args) {
        PolyvCommonLog.d(TAG, "receivePaused");
        if (args != null) {
            String pauseParams = args.getString(0);//获取第一个位置的数据
            try {
                JSONObject jsonObject = new JSONObject(pauseParams);//创建JSON对象
                Boolean pause = jsonObject.getBoolean("paused");
                if (pause) {// 暂停
                    pauseVideo();
                } else {// 播放
                    playVideo();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 初始化配置参数
     *
     * @param args
     */
    private void initConfigParams(ReadableArray args) {
        if (args != null) {
            String params = args.getString(0);//获取第一个位置的数据
            try {
                JSONObject jsonObject = new JSONObject(params);
                String playType = jsonObject.getString("playType");
                this.appId = jsonObject.getString("appId");
                this.appSecret = jsonObject.getString("appSecret");
                this.userId = jsonObject.getString("userId");
                this.channelId = jsonObject.getString("channelId");
                if ("LIVE".equals(playType)) {
                    playMode = PolyvPlayOption.PLAYMODE_LIVE;
                } else {
                    playMode = PolyvPlayOption.PLAYMODE_VOD;
                    this.videoId = jsonObject.getString("videoId");
                }
//                        checkToken(this.userId, this.appSecret, this.channelId, null, this.appId);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 播放视频
     */
    private void playVideo() {
        PolyvCommonLog.d(TAG, "playVideo");
        if (playMode == PolyvPlayOption.PLAYMODE_VOD) {
            PolyvCommonLog.d(TAG, "playVideoVOD");
            if (playbackVideoHelper != null) {
                PolyvCommonLog.d(TAG, "playVideoVODrestart");
                playbackVideoHelper.restartPlay();
            } else {
                PolyvCommonLog.d(TAG, "playVideoVODplay");
                initPlayVideo();
            }
        } else {
            PolyvCommonLog.d(TAG, "playVideoLIVE");
            if (livePlayerHelper != null) {
                PolyvCommonLog.d(TAG, "playVideoVODREstart");
                livePlayerHelper.resume();
            } else {
                PolyvCommonLog.d(TAG, "playVideoVODPlay");
                initPlayVideo();
            }
        }
    }

    private LifecycleEventListener lifecycleEventListener = new LifecycleEventListener() {
        @Override
        public void onHostResume() {
            System.out.println(TAG + "onHostResume");
            if(livePlayerHelper!=null){
                livePlayerHelper.resume();
            }
        }

        @Override
        public void onHostPause() {
            System.out.println(TAG + "onHostPause");
            if(livePlayerHelper!=null){
                livePlayerHelper.pause();
            }
        }

        @Override
        public void onHostDestroy() {
            System.out.println(TAG + "onHostDestroy");
        }
    };
    private void initPlayVideo() {
        checkToken(this.appSecret, this.appId);
        initial();
        initialVideo();
        if (playMode == PolyvPlayOption.PLAYMODE_VOD) {
            initialPlaybackVideo();
            playPlaybackVideo();
        } else {
            initialLiveVideo();
            playPlayLiveVideo();
        }
        intialOretation(); // 监听自动旋转
    }

    private void pauseVideo() {
        switch (playMode) {
            case PolyvPlayOption.PLAYMODE_VOD:
                if (playbackVideoHelper != null)
                    playbackVideoHelper.pause();
                break;
            case PolyvPlayOption.PLAYMODE_LIVE:
                if (livePlayerHelper != null)
                    livePlayerHelper.pause();
                break;
        }
    }
}